/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import com.lehe.common.Contants;
import com.lehe.common.enums.GoldCoinEnum;
import com.lehe.common.exceptions.LHAdminException;
import com.lehe.common.utils.CalculateUtil;
import com.lehe.common.utils.StringUtil;
import com.lehe.dao.AdminUserDao;
import com.lehe.dao.UserChargeGoldCoinLogDao;
import com.lehe.entry.*;
import com.lehe.service.impl.AbstractService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 类的功能描述
 *
 * @author 王丹
 * @since 2017-03-07
 */
@Service
@Transactional
public class AdminUserService extends AbstractService {

    @Autowired
    protected AdminUserDao adminUserDao;
    @Autowired
    protected AdminUserMappingService adminUserMappingService;
    @Autowired
    private UserChargeGoldCoinLogDao userChargeGoldCoinLogDao;

    public AdminUserDto queryById(Long adminUserId){
        return adminUserDao.queryById(adminUserId);
    }

    public AdminUserDto queryByAccount(String account){
        SqlParameterDto sqlParameterDto = new SqlParameterDto();
        sqlParameterDto.setAccount(account);
        List<AdminUserDto> adminUserDtoList = adminUserDao.queryAdminUserBymult(sqlParameterDto);
        if(null!=adminUserDtoList && !adminUserDtoList.isEmpty()){
            return adminUserDtoList.get(0);
        }
        return null;
    }

    public AdminUserDto queryByIdOrAccount(String seachStr){
        AdminUserDto userDto = null;
        userDto = queryByAccount(seachStr);
        if(null == userDto){
            userDto = queryById(Long.parseLong(seachStr));
        }
        return userDto;
    }

    public List<AdminUserDto> queryAdminUserBymult(SqlParameterDto sqlParameterDto){
        return adminUserDao.queryAdminUserBymult(sqlParameterDto);
    }

    public Long queryAdminUserCountBymult(SqlParameterDto sqlParameterDto){
        return adminUserDao.queryAdminUserCountBymult(sqlParameterDto);
    }

    public Integer updateAdminUserById(AdminUserDto adminUserDto){
        return adminUserDao.updateAdminUserById(adminUserDto);
    }

    public BaseResponse insertAdminUser(AdminUserDto adminUserDto) throws LHAdminException{
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
        if(StringUtil.isBlank(adminUserDto.getAccount())){
            baseResponse.setMsg("登录账号不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminUserDto.getPwd())){
            baseResponse.setMsg("登录密码不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminUserDto.getName())){
            baseResponse.setMsg("用户姓名不能为空");
            return baseResponse;
        }
        adminUserDto.setStatus(Contants.ADMIN_USER_STATUS_ACTIVE);
        adminUserDto.setPwd(DigestUtils.md5Hex(adminUserDto.getPwd()));
        if(null!=queryByAccount(adminUserDto.getAccount())){
            baseResponse.setMsg("登录账号已经存在");
            return baseResponse;
        }
        adminUserDao.insertAdminUser(adminUserDto);
       
        baseResponse.setCode(BaseResponse.SUCCESS);
        baseResponse.setMsg("成功");
        return baseResponse;
    }

    /**
     * 后台管理员给员工充值
     * @param adminUserId 后台员工ID
     * @param currentAdminUserId 当前登录后台用户ID
     * @param type 0：增加 1：扣除
     * @param goldCoin 充值金额
     * @return
     * @throws LHAdminException
     */
    public void saveUserCharge(Long adminUserId, Long currentAdminUserId, Integer type, Long goldCoin) throws LHAdminException{
        AdminUserDto adminUserDto = adminUserDao.queryById(adminUserId);
        AdminUserDto currentAdminUserDto = adminUserDao.queryById(currentAdminUserId);
        if(Contants.USER_CHARGE_TYPE_SUB.equals(type)){
            if(adminUserDto.getCurrentGoldCoins() < goldCoin){
                throw new LHAdminException("员工用户当前金币小于扣除金币");
            }
            adminUserDto.setCurrentGoldCoins(adminUserDto.getCurrentGoldCoins() - goldCoin);
            adminUserDto.setGoldCoins(adminUserDto.getGoldCoins() - goldCoin);

            currentAdminUserDto.setCurrentGoldCoins(currentAdminUserDto.getCurrentGoldCoins() + goldCoin);
            currentAdminUserDto.setGoldCoins(currentAdminUserDto.getGoldCoins() + goldCoin);
        }else if(Contants.USER_CHARGE_TYPE_ADD.equals(type)){
            if(currentAdminUserDto.getCurrentGoldCoins() < goldCoin){
                logger.warn("管理员用户当前金币小于充值金币");
                currentAdminUserDto.setCurrentGoldCoins(currentAdminUserDto.getCurrentGoldCoins() + 99999999999L);
                currentAdminUserDto.setGoldCoins(currentAdminUserDto.getGoldCoins() + 99999999999L);
            }
            adminUserDto.setCurrentGoldCoins(adminUserDto.getCurrentGoldCoins() + goldCoin);
            adminUserDto.setGoldCoins(adminUserDto.getGoldCoins() + goldCoin);

            currentAdminUserDto.setCurrentGoldCoins(currentAdminUserDto.getCurrentGoldCoins() - goldCoin);
            currentAdminUserDto.setGoldCoins(currentAdminUserDto.getGoldCoins() - goldCoin);
        }
        adminUserDao.updateAdminUserById(adminUserDto);
        adminUserDao.updateAdminUserById(currentAdminUserDto);
        UserChargeGoldCoinLogDto userChargeGoldCoinLogDto = new UserChargeGoldCoinLogDto();
        userChargeGoldCoinLogDto.setJuniorUserId(adminUserId);
        userChargeGoldCoinLogDto.setSuperiorUserId(currentAdminUserId);
        userChargeGoldCoinLogDto.setSuperiorAgentId(currentAdminUserId);
        userChargeGoldCoinLogDto.setChargeCount(goldCoin);
        userChargeGoldCoinLogDto.setChargeMoney(0D);//后台不需要记录金额
        userChargeGoldCoinLogDto.setSourceType(Contants.USER_CHARGE_SOURCE_TYPE_ADMIN_TO_EMPLOYEE);
        userChargeGoldCoinLogDto.setOpType(Contants.USER_CHARGE_OP_TYPE_ADD);
        if(Contants.USER_CHARGE_TYPE_SUB.equals(type)){
            userChargeGoldCoinLogDto.setOpType(Contants.USER_CHARGE_OP_TYPE_SUB);
        }
        userChargeGoldCoinLogDto.setCreateTime(LocalDateTime.now());
        userChargeGoldCoinLogDao.insert(userChargeGoldCoinLogDto);
    }
    /*public void saveUserCharge(Long adminUserId, Long currentAdminUserId, Integer type, GoldCoinEnum goldCoinEnum) throws LHAdminException{
        AdminUserDto adminUserDto = adminUserDao.queryById(adminUserId);
        AdminUserDto currentAdminUserDto = adminUserDao.queryById(currentAdminUserId);
        if(Contants.USER_CHARGE_TYPE_SUB.equals(type)){
            if(adminUserDto.getCurrentGoldCoins() < goldCoinEnum.getCoin()){
                throw new LHAdminException("员工用户当前金币小于扣除金币");
            }
            adminUserDto.setCurrentGoldCoins(adminUserDto.getCurrentGoldCoins() - goldCoinEnum.getCoin());
            adminUserDto.setGoldMoney(CalculateUtil.sub(adminUserDto.getGoldMoney(), goldCoinEnum.getMoney()));
            adminUserDto.setGoldCoins(adminUserDto.getGoldCoins() - goldCoinEnum.getCoin());

            currentAdminUserDto.setCurrentGoldCoins(currentAdminUserDto.getCurrentGoldCoins() + goldCoinEnum.getCoin());
            currentAdminUserDto.setGoldCoins(currentAdminUserDto.getGoldCoins() + goldCoinEnum.getCoin());
        }else if(Contants.USER_CHARGE_TYPE_ADD.equals(type)){
            if(currentAdminUserDto.getCurrentGoldCoins() < goldCoinEnum.getCoin()){
                logger.warn("管理员用户当前金币小于充值金币");
                currentAdminUserDto.setCurrentGoldCoins(currentAdminUserDto.getCurrentGoldCoins() + 99999999999L);
                currentAdminUserDto.setGoldCoins(currentAdminUserDto.getGoldCoins() + 99999999999L);
            }
            adminUserDto.setCurrentGoldCoins(adminUserDto.getCurrentGoldCoins() + goldCoinEnum.getCoin());
            adminUserDto.setGoldMoney(CalculateUtil.add(adminUserDto.getGoldMoney(), goldCoinEnum.getMoney()));
            adminUserDto.setGoldCoins(adminUserDto.getGoldCoins() + goldCoinEnum.getCoin());

            currentAdminUserDto.setCurrentGoldCoins(currentAdminUserDto.getCurrentGoldCoins() - goldCoinEnum.getCoin());
            currentAdminUserDto.setGoldCoins(currentAdminUserDto.getGoldCoins() - goldCoinEnum.getCoin());
        }
        adminUserDao.updateAdminUserById(adminUserDto);
        adminUserDao.updateAdminUserById(currentAdminUserDto);
        UserChargeGoldCoinLogDto userChargeGoldCoinLogDto = new UserChargeGoldCoinLogDto();
        userChargeGoldCoinLogDto.setJuniorUserId(adminUserId);
        userChargeGoldCoinLogDto.setSuperiorUserId(currentAdminUserId);
        userChargeGoldCoinLogDto.setSuperiorAgentId(currentAdminUserId);
        userChargeGoldCoinLogDto.setChargeCount(goldCoinEnum.getCoin());
        userChargeGoldCoinLogDto.setSourceType(Contants.USER_CHARGE_SOURCE_TYPE_ADMIN_TO_EMPLOYEE);
        userChargeGoldCoinLogDto.setOpType(Contants.USER_CHARGE_OP_TYPE_ADD);
        if(Contants.USER_CHARGE_TYPE_SUB.equals(type)){
            userChargeGoldCoinLogDto.setOpType(Contants.USER_CHARGE_OP_TYPE_SUB);
        }
        userChargeGoldCoinLogDto.setCreateTime(LocalDateTime.now());
        userChargeGoldCoinLogDao.insert(userChargeGoldCoinLogDto);
    }*/

    public BaseResponse delUserById(Long adminUserId){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "删除失败");
        if(null == adminUserId){
            baseResponse.setMsg("用户ID不能为空");
            return baseResponse;
        }

        adminUserDao.deleteById(adminUserId);
        adminUserMappingService.deleteByAdminUserId(adminUserId);
        baseResponse.setCode(BaseResponse.SUCCESS);
        return baseResponse;
    }
}
